<template>
	<el-main>
		<el-card class="box-card" shadow="never">
			<div class="search-box">
				<el-form :inline="true" :model="searchForm" class="demo-form-inline">
					<el-form-item label="分类名称">
						<el-input v-model="searchForm.name" placeholder="" clearable></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="onSubmit" icon="el-icon-search">查询</el-button>
					</el-form-item>
				</el-form>
			</div>
			<div style="width:100%;height:0;border-bottom:#E4E7ED 1px dashed;margin-bottom: 10px"></div>
			<el-button type="primary" icon="el-icon-plus" @click="addBrandCate" style="margin-top: 10px">添加品牌分类
			</el-button>
			<div style="width:100%;height:0;border-bottom:#E4E7ED 1px dashed;margin-top: 10px"></div>
			<el-table
				:data="tableData"
				style="width: 100%;">
				<el-table-column
					prop="id"
					label="ID">
				</el-table-column>
				<el-table-column
					prop="name"
					label="分类名称">
				</el-table-column>
				<el-table-column
					prop="sort"
					label="排序值">
				</el-table-column>
				<el-table-column
					label="状态">
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status == 1">启用</el-tag>
						<el-tag type="danger" v-if="scope.row.status == 2">禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column
					prop="create_time"
					label="创建时间">
				</el-table-column>
				<el-table-column
					prop="operation"
					label="操作">
					<template #default="scope">
						<el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
						<el-button @click="handleDel(scope.row)" type="text" size="small">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<Pages :form="searchForm" :page-obj="page" @pageChange="handlePageChange" @pageJump="getList"></Pages>

		</el-card>

		<save-dialog v-if="dialogVisible" ref="saveDialog" @success="handleSuccess" @closed="dialogVisible=false"
					 :close-on-click-modal="false"></save-dialog>
	</el-main>
</template>

<script>
import saveDialog from "@/views/brandCate/save";
import Pages from "@/components/pages/index.vue";

export default {
	name: '品牌分类管理',
	components: {
		Pages,
		saveDialog
	},
	data() {
		return {
			searchForm: {
				name: '',
				limit: 15,
				page: 1
			},
			dialogVisible: false,
			title: '新增品牌分类',
			page: {
				total: 0
			},
			tableData: [],
		}
	},
	mounted() {
		this.getList()
	},
	methods: {
		// 获取列表
		async getList() {
			let res = await this.$API.brandCate.list.get(this.searchForm)
			this.tableData = res.data.data
			this.page.total = res.data.total
		},
		// 搜索
		onSubmit() {
			this.searchForm.page = 1
			this.getList()
		},
		addBrandCate() {
			this.title = '添加品牌分类'
			this.dialogVisible = true

			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		// 编辑参数
		handleEdit(item) {
			this.title = '编辑品牌分类'
			this.dialogVisible = true

			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(item)
			})
		},
		// 删除参数
		handleDel(item) {
			this.$confirm('此操作将永久删除该品牌分类 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(async () => {
				let res = await this.$API.brandCate.del.get({id: item.id})
				if (res.code == 0) {
					this.$message.success(res.msg)
					this.getList()
				} else {
					this.$message.error(res.msg)
				}
			}).catch(() => {
			});
		},
		// 分页
		handlePageChange(page) {
			this.searchForm.page = page
			this.getList()
		},
		handleSuccess() {
			this.dialogVisible = false
			this.getList()
		}
	}
}
</script>

<style scoped>
</style>